import ListItem from "./ListItem";
import EngineUtil from "./EngineUtil";
import UiManage from "./UiManage";
import ClickEveryDayItem from "./ClickEveryDayItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ClickEveryDayItemCtrl extends ListItem {

@property(cc.Node)
ui: cc.Node = null;
id: any = null;

onLoad() {
    super.onLoad();
    this.onUILoad();
    this.addButtonListen();
}

onUILoad() {
    this.ui = this.node.addComponent(ClickEveryDayItem);
}

addButtonListen() { }

setData(data) {
    this.initData(data);
}

initData(data) {
    if (data) {
        const item = data;
        const taskId = parseInt(item.task_id);
        this.id = item.task_id;
        const dayDes = "累计打卡" + taskId + "日";
        this.ui.dayDes.getComponent(cc.Label).string = taskId === 2 ? "次" : taskId + "";
        this.ui.tipsLabel.getComponent(cc.Label).string = taskId === 2 ? "明日打卡" : dayDes;
        this.ui.rewardCount.getComponent(cc.Label).string = item.reward_type === 1 ? EngineUtil.getCashBalance(item.reward) + "元" : "x" + item.reward;
        const rewardType = "reward_" + item.reward_type;
        UiManage.loadSpriteFrame(this.ui.rewardSp, "clickEveryDay", rewardType, () => {
            this.ui.rewardSp.scale = item.reward_type === 1 ? 0.8 : 1;
        });
        if (h.indexOf(taskId) !== -1) {
            this.ui.spineNode.active = true;
            UiManage.loadSpriteFrame(this.ui.node, "clickEveryDay", "green_bg");
            UiManage.loadSpriteFrame(this.ui.label_bg, "clickEveryDay", "label_green_bg");
            this.ui.rewardCount.color = EngineUtil.getColor("#FFFFFF");
            let labelOutline = this.ui.rewardCount.getComponent(cc.LabelOutline);
            if (!labelOutline) {
                this.ui.rewardCount.addComponent(cc.LabelOutline);
                labelOutline = this.ui.rewardCount.getComponent(cc.LabelOutline);
            }
            labelOutline.width = 1;
            labelOutline.color = EngineUtil.getColor("#853107");
        } else {
            this.ui.spineNode.active = false;
            UiManage.loadSpriteFrame(this.ui.node, "clickEveryDay", "common_bg");
            UiManage.loadSpriteFrame(this.ui.label_bg, "clickEveryDay", "label_common_bg");
        }
    }
}

setCenterAlign() {
    this.ui.rewardCount.getComponent(cc.Label).horizontalAlign = cc.Label.HorizontalAlign.CENTER;
    this.ui.rewardCount.x += 8;
}
}

ClickEveryDayItemCtrl.prefabUrl = "assets/resources/prefabs/ClickEveryDayItem";
ClickEveryDayItemCtrl.className = "ClickEveryDayItemCtrl";